// JavaScript Document
var leftMuTxt = null;
var subMuTxt = null;
var subSubMuTxt = null;

function bodyOnLoad() {
	synLayoutWidth();
}

//------------------------------------------------------------------
//--Function: group checkboxs, one checkbox can control others,
//          select all or select none.
//          Main control checkbox is parent, and orthers is children.
//--Author: LiuJian
//--Date: 2011-6-24
//------------------------------------------------------------------
function clickChild(parentId,childrenName){
    
    var parentCanChecked = true;
    
    var children =document.getElementsByName(childrenName);
    for(var i=0;i<children.length;i++){
        
        if(children[i].checked==false){
            parentCanChecked=false;
            break;
        }
    }
    
    document.getElementById(parentId).checked=parentCanChecked;
}

function clickParent(parentId,childrenName) {
  
  var parentChecked = document.getElementById(parentId).checked;
  var children = document.getElementsByName(childrenName);
  
  for (i = 0; i < children.length; i++) {
      children[i].checked = parentChecked;
  }
}
//------------------------------------------------------------------END



//------------------------------------------------------------------
//--Function: The height of the iframe can resize as the height of the content of itself.
//       
//--Author: LiuJian
//--Date: 2011-6-24
//------------------------------------------------------------------
function iframeResize(thisDoc,iframeId,thisContainor){
	//1.  取得iframe的对象
	var iframeObj = thisDoc.parent.document.getElementById(iframeId);
	
	if(iframeObj != null) {
		var heightValue = 0;
		 
		if(thisContainor!=null && thisContainor!=""){
			//2.  取得本页面显示数据容器对象
			var containor = document.getElementById(thisContainor);
			//3, 下面是取数据表格高度,
			// 也可取本htm文档的高度 ：
		    heightValue = containor.scrollHeight+10;
		    
		}else{
			
		     heightValue = thisDoc.document.body.scrollHeight;
		}
		
		iframeObj.style.height = heightValue + "px";
	}
	
}

//------------------------------------------------------------------END


/* onclick="selectPage(event, this, 'selectEmailTemplate')" */
function selectPage(e, thisObj, _type) {
	if (_type == 'coordinator') {
		popup(e, '../common/select_coordinator_search.html',
				'width:700;height:500');
	}
	if (_type == 'supervisor') {
		popup(e, '../common/select_supervisor_search.html',
				'width:700;height:500');
	}
	if (_type == 'school') {
		popup(e, '../common/select_school_search.html', 'width:700;height:500');
	}
	if (_type == 'schoolAdmin') {
		popup(e, '../common/select_school_admin_search.html',
				'width:700;height:500');
	}
	if (_type == 'principal') {
		popup(e, '../common/select_principal_search.html',
				'width:700;height:500');
	}
	if (_type == 'copySubjectSchedule') {
		popup(e, '../common/select_Schedule_search.html',
				'width:530;height:220');
	}
	if (_type == 'copyExam') {
		popup(e, '../common/select_copy_exam_search.html',
				'width:330;height:200');
	}
	if (_type == 'selectEmailTemplate') {
		popup(e, '../common/select_email_template_search.html',
				'width:700;height:500');
	}
	if (_type == 'role') {
		popup(e, '../common/select_role_search.html', 'width:700;height:500');
	}
	if (_type == 'individual') {
		popup(e, '../common/select_indiv_search.html', 'width:700;height:500');
	}
	if (_type == 'user') {
		popup(e, '../common/select_user_search.html', 'width:700;height:500');
	}
	if (_type == 'select_centre') {
		popup(e, '../common/select_multiple_centre_search.html',
				'width:700;height:500');
	}
	if (_type == 'select_single_candidate') {
		popup(e, '../common/select_single_candidate_search.html',
				'width:700;height:500');
	}
	if (_type == 'select_multiple_candidate') {
		popup(e, '../common/select_multiple_candidate_search.html',
				'width:700;height:500');
	}
	if (_type == 'select_single_user') {
		popup(e, '../common/select_single_user_search.html',
				'width:700;height:500');
	}
	if (_type == 'select_multiple_user') {
		popup(e, '../common/select_multiple_user_search.html',
				'width:700;height:500');
	}
}

function hideIfSelected(obj, valueType, value, targetId) {
	var _selected = CE.selected(obj, valueType);
	var _target = CE.$(targetId);
	if (_selected == value) {
		_target.style.display = "none";
	} else {
		_target.style.display = "";
	}
}

function showIfSelected(obj, valueType, value, targetId) {
	var _selected = CE.selected(obj, valueType);
	var _target = CE.$(targetId);
	if (_selected == value) {
		_target.style.display = "";
	} else {
		_target.style.display = "none";
	}
}

function hideOrShow(targetId) {
	var _target = CE.$(targetId);
	if (_target.style.display == "none") {
		_target.style.display = "";
	} else {
		_target.style.display = "none";
	}
}
function hide(targetId) {
	var _target = CE.$(targetId);
	_target.style.display = "none";
}
function show(targetId) {
	var _target = CE.$(targetId);
	_target.style.display = "";
}

function changeRole(module) {
	if (confirm("Please save all data before changing role?")) {
		window.location = "../" + module + "/select_role.html";
	}
}

function showIfChecked(obj, targetId) {
	var _target = CE.$(targetId);
	if (obj.checked == true) {
		_target.style.display = "";
	} else {
		_target.style.display = "none";
	}
}

function logout() {
	window.location = "login.html";
}

function actionOnLogo() {
	window.location = "../index.html";
}

function changeFont(_pNum) {
	STYLE.changeFontSize(_pNum);
}

function selectTab(menuId, tabId) {
	var _tab = CE.$(menuId);
	var cells = _tab.rows[0].cells;
	for ( var i = 0; i < cells.length; i++) {
		if (cells[i].id == tabId) {
			cells[i].className = "item2";
		}
	}
}

function selectGetOptGroupOnChange(_thisObj, tgtName) {
	var _selectedOpt = _thisObj.options[_thisObj.selectedIndex];
	var _parentOptGroup = CE.getParentByTagName(_selectedOpt, "optgroup");
	if (_parentOptGroup.label != "" && _parentOptGroup.label != undefined) {
		CE.$(tgtName).innerHTML = _parentOptGroup.label;
	}
}

function collapse(thisObj) {
	var _tr = thisObj;
	var _table = CE.getParentByTagName(thisObj, "table");
	var _c = _tr.rowIndex;
	var _t = _table.rows.length;
	for ( var i = _c + 1; i < _t; i++) {
		var _cTr = _table.rows[i];
		if (_tr.className == _cTr.className) {
			break;
		} else {
			if (_cTr.style.display == "") {
				_cTr.style.display = "none";
			} else {
				_cTr.style.display = "";
			}
		}
	}
}

//function resetScrollDivWidth(divLeft, divRight){
//	var _divLeft = CE.$(divLeft);
// var _divRight = CE.$(divRight);
// _divLeft.style.border = "1px solid red";
// CE.pty(_divLeft);
// alert(_divLeft.W);
// }

function synLayoutWidth() {
	var _lftMenuOffset = 254;
	var _lftMu = CE.getElementsByClassName(document, "div", "lftMu", true);
	if (_lftMu == "")
		_lftMenuOffset = 0;
	var _bodys = CE.getElementsByClassName(document, "div", "body", true);
	var _body = _bodys[0];
	var _bodyW = CE.pty(_bodys[0]).W;
	var _sLists = CE.getElementsByClassName(document, "div", "sList", true);
	var _maxWidth = 0;
	for ( var i = 0; i < _sLists.length; i++) {
		var _tW = CE.pty(_sLists[i]).W;
		if (_tW > _maxWidth) {
			//	alert(_tW);
			_maxWidth = _tW;
		}
	}

	var _sForm1s = CE.getElementsByClassName(document, "div", "form1", true);
	for ( var i = 0; i < _sForm1s.length; i++) {
		var _tW = CE.pty(_sForm1s[i]).W;
		if (_tW > _maxWidth) {
			_maxWidth = _tW;
		}
	}
	var _sForm2s = CE.getElementsByClassName(document, "div", "form2", true);
	for ( var i = 0; i < _sForm2s.length; i++) {
		var _tW = CE.pty(_sForm2s[i]).W;
		if (_tW > _maxWidth) {
			_maxWidth = _tW;
		}
	}
	//alert((_maxWidth + _lftMenuOffset) +"_"+ _bodyW);			
	if ((_maxWidth + _lftMenuOffset) > _bodyW) {
		_body.style.width = _maxWidth + _lftMenuOffset + "px";
	} else {
		_body.style.width = ((CE.pty(_bodys[0]).W < 1003) ? 1003 : _bodyW)
				+ "px";
	}
}

function popImages(e) {
	var _s_form = CE.createElement("form");
	_s_form.target = "_blank";
	document.body.appendChild(_s_form);
	_s_form.action = "../common/pop_images.html";
	_s_form.submit();
	// popup(e, '../common/pop_images.html', 'width:700;height:500');
}
function popPdfs(e) {
	//popup(e, '../common/select_school_admin_search.html', 'width:700;height:500');
}
function popDocs(e) {
	//popup(e, '../common/select_school_admin_search.html', 'width:700;height:500');
}

function expandNextRow(thisObj) {

}

function noOfCbxChecked(ctr) {
	var ctrObj = CE.$(ctr);
	var cbxs = CE
			.getElementsByClassName(ctrObj, "input", "inputCheckbox", true);
	var _c = 0;
	for ( var i = 0; i < cbxs.length; i++) {
		if (cbxs[i].checked) {
			_c++;
		}
	}
	return _c;
}

function validateHKID(hkid) {

	if (hkid == "") {
		return false;
	}
	hkid = hkid.toUpperCase();
	var idcard_array = hkid.split("");

	if (idcard_array.length != 10) {
		return false;
	}
   
	if (!(idcard_array[0] >= 'A' && idcard_array[0] <= 'Z')) {
		return false;
	}

	var first = idcard_array[0];

	var total = ((first.charCodeAt(first) - 64) % 11) * 8;

	for ( var i = 7; i > 1; i--) {
		var j = parseInt(idcard_array[8 - i]) * i;
		total = total + j;
	}
	var left;
	if((total % 11)==0){
		left=0;
	}else{
	    left = 11 - (total % 11);
	}
	if (idcard_array[8] != 'A'&&left == parseInt(idcard_array[8])) {
		return true;
	} else if(idcard_array[8] == 'A'&&left == 10){
		return true;
	}else {
		return false;
	}

}
function validateDate(value){
	var isDate = true;
	if(value!=null){
		 value = value.replace(/(^\s*)|(\s*$)/g, "");
		 if(value!=""){
	       var detectedDateFormat = DE.getDateFormat(value);
	       if(detectedDateFormat == null){
	  	       isDate = false;
	       }
	      if(detectedDateFormat!="dd,mm,yyyy,-"){
		       isDate = false;
	      }
	      var dateSplit = value.split("-");
	      var year=dateSplit[2];
	      var month=dateSplit[1];
	      var day=dateSplit[0];
	      if (!(((year % 400)==0)||((year % 100!=0)&&(year % 4 ==0 ))) && (month==2) && (day>=29)){
		       isDate = false;
	      }
	      if ((month==2) && (day>=30)){
		       isDate = false;
	      }
	      if ((month<=7) && ((month % 2)==0) && (day>=31)){
		       isDate = false;
	      }
	      if ((month>=8) && ((month % 2)==1) && (day>=31)){
		       isDate = false;
	      }
	   }
	}
	return isDate;
}
// JavaScript Document